/************************************************************
 * Filename: Graphics.h
 * Purpose: This system handles the drawing of game objects.
 * Author: Jeremy Kings
 * Revision: 3 (3 December 2010)
 *
 * Copyright 2010, Digipen Institute of Technology
 ************************************************************/
#pragma once

#include "System.h"
#include "Geometry.h"
#include "Matrix4.h"
#include "Sprite.h"
#include "Camera.h"
#include "Emitter.h"

namespace Engine
{
	typedef IDirect3DTexture9 Texture;
	typedef IDirect3DVertexBuffer9 VertexBuffer;
	typedef std::map<GameObjectType, Texture*> TextureMap;
	typedef std::map<unsigned, Sprite*> SpriteMap;
	typedef std::map<unsigned, Emitter*> EmitterMap;

	enum GameObjectType; // forward declaration of game object types

	class Graphics : public System
	{
	public:
		Graphics(HWND hWnd, int clientWidth, int clientHeight);
		virtual void Load(); // load assets, etc.
		virtual void ReceiveMessage(Message* msg);
		virtual void Update(float frameTime); // update transformations, draw everything
		virtual void Unload(); // unload assets
		~Graphics();

		Vector2D ScreenToWorld(Vector2D pos); // converts a position vector from screen to world space
	private:
		void InitD3D(); // initialize Direct3D
		void LoadTexture(std::string filename, GameObjectType type);
		void LoadAssets();
		void SetupMatrices();
		void InitGeometry();
		void Render(float frameTime);
		void DrawBackground();
		void DebugDraw();

	public:
		friend class Sprite;
		friend class Emitter;
		friend class DebugDrawer;
	private:
		HWND hWnd; // window handle
		int ScreenWidth, ScreenHeight; // Screen size
		Vector2D SurfaceSize; // Size of the drawing surface

		// Direct3D pointers
		IDirect3D9* pD3D;
		IDirect3DDevice9* pDevice;
		D3DPRESENT_PARAMETERS PresentParams;
		
		VertexBuffer* pSpriteBuffer;		 // for regular sprites
		VertexBuffer* pBackgroundBuffer; // for background layers
		VertexBuffer* pParticleBuffer;	 // for drawing particle effects

		TextureMap Textures;  // Textures for all game objects

		SpriteMap Sprites;  // List of sprite components that belong to game objects
		Camera* pCamera;		// Current camera object -- determines viewing params
		Vector3D CameraPos;
		EmitterMap Emitters; // All emitter components -- need to be updated each frame

		// Projection and view matrices
		Matrix4 ProjMatrix;
		Matrix4 ViewMatrix;
		Matrix4 ViewProjMatrix;
	};
}
